//
//  CAPromotionViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/15.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAPromotionViewController.h"
#import "CAHeaderView.h"
#import "CAMoneyView.h"
#import "CAUpgradeView.h"
#import "CASchoolView.h"
#import "CANoticeTableViewCell.h"
#import "CANoticeHeaderView.h"
#import "CANoticeFooterView.h"
#import "CAUpgradeViewController.h"
#import "CAPromotionApi.h"
#import "CALoginViewController.h"
#import "CAAddUserInfoViewController.h"

@interface CAPromotionViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *containterView;
@property (nonatomic, strong) CAHeaderView *headerView;
@property (nonatomic, strong) CAMoneyView *moneyView;
@property (nonatomic, strong) CAUpgradeView *upgradeView;
@property (nonatomic, strong) CASchoolView *schoolView;
@property (nonatomic, strong) CANoticeHeaderView *noticeHeaderView;
@property (nonatomic, strong) CANoticeFooterView *noticeFooterView;


@property (nonatomic, strong) NSArray *noticeArray;
@property (nonatomic, strong) UITableView *noticeTableView;

@end

static NSString *cellIdentifier = @"NoticeCell";

@implementation CAPromotionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)lm_initialDefaultsForController
{
}

- (void)lm_configNavigationForController
{
    self.navigationItem.title = @"推广中心";
    self.navigationController.navigationBar.tintColor = RGBCOLOR(113, 113, 113);
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : RGBCOLOR(35, 24, 21)};
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)lm_createViewForController
{
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.containterView];
    [self.containterView addSubview:self.headerView];
    [self.containterView addSubview:self.moneyView];
    [self.containterView addSubview:self.upgradeView];
    [self.containterView addSubview:self.schoolView];
    [self.containterView addSubview:self.noticeTableView];
}

-(void)lm_bindViewModelForController
{
    
}

#pragma mark - Layout

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_containterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containterView);
        make.top.equalTo(self.containterView);
        make.height.mas_equalTo(190);
    }];
    
    [_moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containterView);
        make.top.equalTo(self.headerView.mas_bottom).offset(10);
        make.height.mas_equalTo(150);
    }];
    
    [_upgradeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containterView);
        make.top.equalTo(self.moneyView.mas_bottom);
        make.height.mas_equalTo(175);
    }];
    
    [_schoolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containterView);
        make.top.equalTo(self.upgradeView.mas_bottom).offset(10);
        make.height.mas_equalTo(120);
    }];
    
    [_noticeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.containterView);
        make.top.equalTo(self.schoolView.mas_bottom);
        make.height.mas_equalTo(330);
    }];
    
    [_containterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.noticeTableView).offset(80);
    }];
}

#pragma mark - Getter
- (UIScrollView *) scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

- (UIView *) containterView {
    if (!_containterView) {
        _containterView = [UIView new];
        _containterView.backgroundColor = [UIColor clearColor];
    }
    return _containterView;
}

- (CAHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [CAHeaderView new];
    }
    return _headerView;
}

- (CAMoneyView *)moneyView {
    if (!_moneyView) {
        _moneyView = [CAMoneyView new];
    }
    return _moneyView;
}

- (CAUpgradeView *)upgradeView {
    if (!_upgradeView) {
        _upgradeView = [CAUpgradeView new];
        [_upgradeView.upgradeButton addTarget:self action:@selector(upgradeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _upgradeView;
}

- (CASchoolView *)schoolView {
    if (!_schoolView) {
        _schoolView = [CASchoolView new];
    }
    return _schoolView;
}

- (UITableView *) noticeTableView{
    if (!_noticeTableView) {
        _noticeTableView = [UITableView new];
        _noticeTableView.dataSource = self;
        _noticeTableView.delegate = self;
        _noticeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_noticeTableView registerClass:[CANoticeTableViewCell class] forCellReuseIdentifier:cellIdentifier];
        _noticeTableView.scrollEnabled = NO;
        _noticeTableView.tableHeaderView = self.noticeHeaderView;
        _noticeTableView.tableFooterView = self.noticeFooterView;
    }
    return _noticeTableView;
}

- (CANoticeHeaderView *)noticeHeaderView {
    if (!_noticeHeaderView) {
        _noticeHeaderView = [[CANoticeHeaderView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, 25}];
    }
    return _noticeHeaderView;
}

- (CANoticeFooterView *)noticeFooterView {
    if (!_noticeFooterView) {
        _noticeFooterView = [[CANoticeFooterView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, 65}];
    }
    return _noticeFooterView;
}

#pragma mark private method
- (void) loadData {
    CAPromotionApi *promotionApi = [[CAPromotionApi alloc] init];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [promotionApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAPromotionApi *api = (CAPromotionApi *) request;
        if (api.done) {
            NSLog(@"%@", api.responseObject);
            NSString *logoStr = api.responseObject[@"result"][@"user"][@"portrait"];
            if (!IsStrEmpty(logoStr)) {
                NSString *logoUrl = [NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, [NSURL URLWithString:api.responseObject[@"result"][@"user"][@"portrait"]]];
                [_headerView.logoImageView sd_setImageWithURL:[NSURL URLWithString:logoUrl]];
            }
            _headerView.nameLabel.text = api.responseObject[@"result"][@"user"][@"name"];
            
            _moneyView.dayMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [api.responseObject[@"result"][@"myIncome"][@"day"] floatValue]];
            _moneyView.monthMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [api.responseObject[@"result"][@"myIncome"][@"month"] floatValue]];
            _moneyView.aboutMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [api.responseObject[@"result"][@"myIncome"][@"count"] floatValue]];
            _moneyView.cashMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [api.responseObject[@"result"][@"myIncome"][@"money"] floatValue]];
            
            _noticeArray = api.responseObject[@"result"][@"noticeList"];
            [_noticeTableView reloadData];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)upgradeAction {
    NSLog(@"%@",GVUD.cid);
    if (GVUD.isLogin) {
        if (IsStrEmpty(GVUD.cid)) {
            CAAddUserInfoViewController *userInfoVC = [CAAddUserInfoViewController new];
            userInfoVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:userInfoVC animated:YES];
        } else {
            CAUpgradeViewController *upgradeVC = [CAUpgradeViewController new];
            upgradeVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:upgradeVC animated:YES];
        }
    } else {
        CALoginViewController *loginVC = [CALoginViewController new];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

#pragma mark - Table view DataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CANoticeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _noticeArray[indexPath.row];
    
    cell.titleLabel.text = dict[@"title"];
    cell.dateLabel.text = dict[@"created_at"];
    cell.readLabel.text = [NSString stringWithFormat:@"%@人已读", dict[@"read_number"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSDictionary *dict = self.noticeArray[indexPath.row];
//    CAGoodsListViewController *goodsListVC = [CAGoodsListViewController new];
//    goodsListVC.vcTitle = dict[@"name"];
//    goodsListVC.category = [dict[@"id"] integerValue];
//    [self.navigationController pushViewController:goodsListVC animated:YES];
}


@end
